from selenium.webdriver.common.by import By
from base_page.basepage import BasePage
from utils import DriverUtils
import allure
import time


# 对象层

class MyPage(BasePage):
    def __init__(self,type="app"):
        # 继承基类的属性
        # super().__init__()
        super().__init__(type)
        self.driver.switch_to.context(self.driver.contexts[-1])
        self.My = ("css", ".am-tab-bar-tab-title")      # 获取我的按钮
        self.passage = ("xpath", "//*[text()= '修改机构信息']")    # 获取员工选项
        self.revase = ("xpath", "//*[text()= '修改']")    # 获取修改
        self.type = ("css", "div.am-list-extra")     # 获取修改页面的员工类型
    # 获取我的按钮
    def find_My(self):
        return self.find_elements(self.My)[-1]

    # 获取员工选项
    def find_passage(self):
        return self.find_element(self.passage)

    # 获取修改
    def find_revase(self):
        return self.find_element(self.revase)

    # 获取修改页面的员工类型
    def find_type(self):
        return self.find_element(self.type)

    # 选择后的确定按钮
    def find_type_yes(self):
        return self.find_element_if("css", "div.am-picker-popup-item.am-picker-popup-header-right")

    def find_yes(self):
        return self.find_element_if("css", "a.am-button.am-button-primary")

    # 获取员工类型
    def find_ygvalue(self):
        return self.find_element_if("xpath", "/html/body/div/div/div[4]/div[2]/div[1]/div/div[2]/input")

    # 定位修改里面的 员工工号
    def find_set_sum(self):
        return self.find_element_if('xpath', '//input[@id = "staffId"]')

    def find_toast(self):
        return self.find_element_if("id", "text")

    # 机构页面的员工号
    def find_sum(self):
        return self.find_element_if("xpath", "/html/body/div/div/div[4]/div[2]/div[2]/div/div[2]/input")

    # 获取员工姓名
    def find_name(self):
        return self.find_element_if("xpath", "/html/body/div/div/div[4]/div[2]/div[3]/div/div[2]/input")

    def switch_to(self, sum):
        return self.driver.switch_to.context(self.driver.contexts[sum])

    def find_move_t(self):
        return self.driver.swipe(800, 1300, 800, 1200, 1600)

    def find_move_b(self):
        return self.driver.swipe(800, 1200, 800, 1300, 1600)
    # 所属机构
    def find_comp(self):
        return self.find_element_if("xpath", "/html/body/div/div/div[5]/div[2]/div[1]/div/div[1]/div[2]")


# 操作层
class MyHandle(object):
    def __init__(self):
        self.page = MyPage()

    def click_my(self):
        self.page.find_My().click()  # 点击我的

    def click_passage(self):
        self.page.find_passage().click()  # 点击修改机构信息

    def click_revase(self):
        self.page.find_revase().click()  # 点击修改

    def click_type(self):
        self.page.find_type().click()  # 点击员工类型

    def click_type_yes(self):
        self.page.find_type_yes().click()  # 点击员工类型确定

    def click_yes(self):
        self.page.find_yes().click()  # 点击确定

    def get_ygvalue(self):
        return self.page.find_ygvalue().get_attribute("value")  # 获取员工类型的值

    def clear(self):
        self.page.find_set_sum().clear()

    def send_key(self, text):
        self.page.find_set_sum().send_keys(text)

    def get_value_sum(self):
        return self.page.find_sum().get_attribute("value")  # 获取员工工号

    def get_name(self):
        return self.page.find_name().get_attribute("value")  # 获取员工姓名

    def click_comp(self):
        self.page.find_comp().click()


# 逻辑层
class MyProxy(object):
    def __init__(self):
        self.handle = MyHandle()
        self.page = MyPage()
        self.driver = DriverUtils.get_app_driver()

    def revase_type(self):
        self.handle.click_my()  # 点击我的
        time.sleep(3)
        self.handle.click_passage()  # 点击修改机构
        time.sleep(3)
        self.handle.click_revase()  # 点击修改
        time.sleep(20)

        self.handle.click_type()  # 点击 员工类型
        time.sleep(2)
        self.page.switch_to(0)  # 切换
        time.sleep(2)
        self.page.find_move_t()  # 向上滑动100像素
        time.sleep(2)
        self.page.switch_to(-1)
        time.sleep(2)
        self.handle.click_type_yes()  # 点击确定选择
        time.sleep(2)
        self.handle.click_yes()  # 点击确定
        time.sleep(4)

    def revase_sum(self, text):
        self.handle.click_my()  # 点击我的

        time.sleep(3)
        self.handle.click_passage()  # 点击修改机构
        time.sleep(3)
        self.handle.click_revase()  # 点击修改
        time.sleep(20)
        self.handle.clear()  # 清除 工号
        time.sleep(2)
        self.handle.send_key(text)  # 输入工号
        time.sleep(2)
        self.handle.click_yes()  # 点击确定
        time.sleep(4)

    def revase_sum_none(self):
        self.handle.click_my()  # 点击我的

        time.sleep(3)
        self.handle.click_passage()  # 点击修改机构
        time.sleep(3)
        self.handle.click_revase()  # 点击修改
        time.sleep(20)
        self.handle.clear()  # 清除 工号
        time.sleep(2)
        self.handle.click_yes()  # 点击确定
        time.sleep(4)

    def revase_comp(self):
        self.handle.click_my()  # 点击我的
        time.sleep(3)
        self.handle.click_passage()  # 点击修改机构
        time.sleep(3)
        self.handle.click_revase()  # 点击修改
        time.sleep(20)

        self.handle.click_comp()  # 点击 所属公司
        time.sleep(2)
        # self.page.switch_to(0)  # 切换
        # time.sleep(2)
        # self.page.find_move_t()  # 向上滑动100像素
        # time.sleep(2)
        # self.page.switch_to(-1)
        # time.sleep(2)
        # self.handle.click_type_yes()  # 点击确定  选择
        # time.sleep(2)
        # self.handle.click_yes()  # 点击确定
        # time.sleep(4)
    def swito_top(self):
        self.page.switch_to(0)  # 切换
        time.sleep(2)
        self.page.find_move_t()  # 向上滑动100像素
        time.sleep(2)
        self.page.switch_to(-1)
        time.sleep(2)
    def swito_bottom(self):
        self.page.switch_to(0)  # 切换
        time.sleep(2)
        self.page.find_move_b()  # 向上滑动100像素
        time.sleep(2)
        self.page.switch_to(-1)
        time.sleep(2)
    def yes_(self):
        self.handle.click_type_yes()  # 点击确定  选择
        time.sleep(2)
        self.handle.click_yes()  # 点击确定
        time.sleep(4)
